﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

/// <summary>
/// 主界面UI管理
/// </summary>
public class MenuUIManager : MonoBehaviour
{
    public static MenuUIManager Instance;

    // UI面板
    public Transform settingPanel;
    public Transform settingContent;
    [Header("关卡界面内容")]
    public Transform levelPanel;
    public Transform levelTitle;
    public Transform levelView;
    [Header("教程界面内容")]
    public Transform tutorialPanel;
    public Transform tutorialContent;
    public GameObject[] tutorials;

    public Text soundText;
    public GameObject soundON;
    public GameObject soundOFF;

    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {
        if (AudioManager.Instance.Sound)
            soundText.text = "声音 开";
        else
            soundText.text = "声音 关";
        soundON.SetActive(AudioManager.Instance.Sound);
        soundOFF.SetActive(!AudioManager.Instance.Sound);
    }

    //首界面固定
    [RuntimeInitializeOnLoadMethod]
    static void Initialize()
    {
        string startSceneName = "Menu";
        Scene scene = SceneManager.GetActiveScene();
        if (scene.name.Equals(startSceneName))
        {
            return;
        }
        SceneManager.LoadScene(startSceneName);
    }

    /// <summary>
    /// 设置按钮
    /// </summary>
    public void SettingBtnClicked()
    {
        settingPanel.localScale = settingContent.localScale = new Vector3(0, 0, 1);
        settingPanel.gameObject.SetActive(true);
        DOTween.To(() => settingPanel.localScale, x => settingPanel.localScale = x, new Vector3(1, 1, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
        {
            DOTween.To(() => settingContent.localScale, x => settingContent.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).SetUpdate(true);
        });
    }

    /// <summary>
    /// 声音控制
    /// </summary>
    public void SoundBtnClicked()
    {
        if (AudioManager.Instance.Sound)
        {
            AudioManager.Instance.Sound = false;
            soundText.text = "声音 关";
        }
        else
        {
            AudioManager.Instance.Sound = true;
            soundText.text = "声音 开";
        }
        soundON.SetActive(AudioManager.Instance.Sound);
        soundOFF.SetActive(!AudioManager.Instance.Sound);
    }

    /// <summary>
    /// 关闭设置
    /// </summary>
    public void HideSettings()
    {
        DOTween.To(() => settingContent.localScale, x => settingContent.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
        {
            DOTween.To(() => settingPanel.localScale, x => settingPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                settingPanel.gameObject.SetActive(false);
            });
        });
    }

    /// <summary>
    /// 开始按钮
    /// </summary>
    public void PlayBtnClicked()
    {
        levelPanel.localScale = levelTitle.localScale = levelView.localScale = new Vector3(0, 0, 1);
        levelPanel.gameObject.SetActive(true);
        DOTween.To(() => levelPanel.localScale, x => levelPanel.localScale = x, new Vector3(1, 1, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
        {
            levelView.GetComponent<ScrollRect>().DOVerticalNormalizedPos(1, 0.5f);
            DOTween.To(() => levelView.localScale, x => levelView.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).SetUpdate(true);
            DOTween.To(() => levelTitle.localScale, x => levelTitle.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).SetUpdate(true);
        });
    }

    /// <summary>
    /// 关闭关卡选择
    /// </summary>
    public void HideLevels()
    {
        DOTween.To(() => levelView.localScale, x => levelView.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).SetUpdate(true);
        DOTween.To(() => levelTitle.localScale, x => levelTitle.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).SetUpdate(true).OnComplete(() =>
        {
            DOTween.To(() => levelPanel.localScale, x => levelPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                levelPanel.gameObject.SetActive(false);
            });
        });
    }

    /// <summary>
    /// 显示教程说明
    /// </summary>
    public void ShowToturial()
    {
        tutorialPanel.localScale = tutorialContent.localScale = new Vector3(0, 0, 1);
        tutorialPanel.gameObject.SetActive(true);
        DOTween.To(() => tutorialPanel.localScale, x => tutorialPanel.localScale = x, new Vector3(1, 1, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
        {
            tutorials[Random.Range(0, 3)].SetActive(true);
            DOTween.To(() => tutorialContent.localScale, x => tutorialContent.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).SetUpdate(true);
        });
    }

    /// <summary>
    /// 进入游戏
    /// </summary>
    public void LoadGame()
    {
        DOTween.To(() => tutorialContent.localScale, x => tutorialContent.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).SetUpdate(true).OnComplete(() =>
        {
            SceneManager.LoadScene("Game");
        });
    }

    /// <summary>
    /// 按钮音效
    /// </summary>
    public void PlayButtonSound()
    {
        AudioManager.Instance.PlayButtonSound();
    }
}
